package com.tenglong.teacher.ui.homework.msgviewholder;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.netease.nim.uikit.common.util.sys.ScreenUtil;
import com.netease.nimlib.sdk.msg.attachment.FileAttachment;
import com.netease.nimlib.sdk.msg.attachment.ImageAttachment;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.tenglong.teacher.R;
import com.tenglong.teacher.ui.common.view.RoundImageView;
import com.tenglong.teacher.ui.homework.HomeWorkDetailAdapter;

import java.io.File;


public class HWPicViewHolder extends HWBaseViewHolder {
    protected RoundImageView thumbnail;
    private HomeWorkDetailAdapter.HomeWorkDetaiListener listener;

    public HWPicViewHolder(View itemView) {
        super(itemView);
        thumbnail = itemView.findViewById(com.netease.nim.uikit.R.id.message_item_thumb_thumbnail);
        thumbnail.mBorderRadius = 0;
        thumbnail.setImageRotate(false);
    }

    public HWPicViewHolder(View itemView, HomeWorkDetailAdapter.HomeWorkDetaiListener listener) {
        this(itemView);
        this.listener = listener;
    }

    public void onBindViewHolder(IMMessage msg) {
        super.onBindViewHolder(msg);
        FileAttachment msgAttachment = (FileAttachment) message.getAttachment();
        String path = msgAttachment.getPath();
        String thumbPath = msgAttachment.getThumbPath();
        String thumbUrl = null;
        if (msgAttachment instanceof ImageAttachment) {
            thumbUrl = ((ImageAttachment) msgAttachment).getThumbUrl();
        }
        String url = msgAttachment.getUrl();
        if (!TextUtils.isEmpty(thumbPath)) {
            loadAsPath(thumbPath, getImageMaxEdge(), getImageMaxEdge());
        } else if (!TextUtils.isEmpty(path)) {
            loadAsPath(thumbFromSourceFile(path), getImageMaxEdge(), getImageMaxEdge());
        } else if (!TextUtils.isEmpty(thumbUrl)) {
            loadAsUri(thumbUrl);
        } else if (!TextUtils.isEmpty(url)) {
            loadAsUri(url);
        } else {
            loadAsPath(null, getImageMaxEdge(), getImageMaxEdge());
            if (message.getAttachStatus() == AttachStatusEnum.transferred
                    || message.getAttachStatus() == AttachStatusEnum.def) {
                downloadAttachment();
            }
        }
    }


    public static int getImageMaxEdge() {
        return (int) (165.0 / 320.0 * ScreenUtil.screenWidth);
    }

    protected String thumbFromSourceFile(String path) {
        return path;
    }

    protected void onItemClick(View v) {
//        Intent intent = new Intent(mContext, ZoomPicAcitivity.class);
//        intent.putExtra("uri", ((ImageAttachment) message.getAttachment()).getThumbUrl());
//        mContext.startActivity(intent);
        if (listener != null) {
            listener.onPhotoListener(message);
        }
    }

    /**
     * 加载uri资源
     *
     * @param uri
     */
    public void loadAsUri(String uri) {
        RequestOptions options = new RequestOptions()
                .priority(Priority.HIGH)
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .centerCrop()
                .placeholder(com.netease.nim.uikit.R.drawable.nim_image_default)
                .error(com.netease.nim.uikit.R.drawable.nim_image_default);
        Glide.with(mContext)
                .load(uri)
                .apply(options)
                .into(thumbnail);
    }

    /**
     * 加载res资源
     *
     * @param resId
     */
    public void loadAsResource(final int resId) {
        Glide.with(mContext).load(resId).into(thumbnail);
    }

    /**
     * 加载本地资源
     *
     * @param path
     * @param width
     * @param height
     */
    public void loadAsPath(final String path, final int width, final int height) {
        if (TextUtils.isEmpty(path)) {
            loadAsResource(R.drawable.nim_image_default);
            return;
        }
        //.priority(Priority.HIGH)
        //                .format(DecodeFormat.PREFER_RGB_565)
        //                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);


        RequestOptions options = new RequestOptions()
                .priority(Priority.HIGH)
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .override(width, height)
                .centerCrop()
                .placeholder(R.drawable.nim_image_default)
                .error(R.drawable.nim_image_default);
        Glide.with(mContext)
                .asBitmap()
                .apply(options)
                .load(new File(path))
                .into(thumbnail);
    }
}
